#include "app_json.h"

/*

{
  "action": "start",
  "connetType": "wifi",
  "motorId": 2,
  "motorSpeed": 1200,
  "status": "running"
}


*/

/// @brief 解析cjson
/// @param data 传入raw原始文本
/// @param app_json_data 用于接收解析数据结构体
/// @return 返回com值
Com_state_t app_json_unpack(char *data, app_json_t *app_json_data)
{
    // 创建json对象
    cJSON *root = cJSON_Parse(data);
    if (root == NULL)
    {
        log_info("解析失败,json语法非法或其他原因。。。。");
        return Com_FAIL;
    }

    // 获取各个字段并填充到结构体
    cJSON *action = cJSON_GetObjectItem(root, "action");
    cJSON *connetType = cJSON_GetObjectItem(root, "connetType");
    cJSON *motorId = cJSON_GetObjectItem(root, "motorId");
    cJSON *motorSpeed = cJSON_GetObjectItem(root, "motorSpeed");
    cJSON *status = cJSON_GetObjectItem(root, "status");

    // 检查并填充数据
    if (cJSON_IsString(action) && action->valuestring != NULL)
    {
        strncpy(app_json_data->action, action->valuestring, sizeof(app_json_data->action) - 1);
        app_json_data->action[sizeof(app_json_data->action) - 1] = '\0'; // 确保字符串以 '\0' 结尾
    }
    else
    {
        log_info("action 字段缺失或格式不正确");
        cJSON_Delete(root);
        return Com_FAIL;
    }

    if (cJSON_IsString(connetType) && connetType->valuestring != NULL)
    {
        strncpy(app_json_data->connetType, connetType->valuestring, sizeof(app_json_data->connetType) - 1);
        app_json_data->connetType[sizeof(app_json_data->connetType) - 1] = '\0';
    }
    else
    {
        log_info("connetType 字段缺失或格式不正确");
        cJSON_Delete(root);
        return Com_FAIL;
    }

    if (cJSON_IsNumber(motorId))
    {
        app_json_data->motorId = motorId->valueint;
    }
    else
    {
        log_info("motorId 字段缺失或格式不正确");
        cJSON_Delete(root);
        return Com_FAIL;
    }

    if (cJSON_IsNumber(motorSpeed))
    {
        app_json_data->motorSpeed = motorSpeed->valueint;
    }
    else
    {
        log_info("motorSpeed 字段缺失或格式不正确");
        cJSON_Delete(root);
        return Com_FAIL;
    }

    if (cJSON_IsString(status) && status->valuestring != NULL)
    {
        strncpy(app_json_data->status, status->valuestring, sizeof(app_json_data->status) - 1);
        app_json_data->status[sizeof(app_json_data->status) - 1] = '\0';
    }
    else
    {
        log_info("status 字段缺失或格式不正确");
        cJSON_Delete(root);
        return Com_FAIL;
    }

    // 释放 JSON 对象
    cJSON_Delete(root);

    return Com_OK;
}

/// @brief 将app_json结构体数据打包字符串发走
/// @param app_json_data
/// @param data 字符串
/// @return
Com_state_t app_json_depack(app_json_t *app_json_data, char* data)
{
    // 创建一个空的 JSON 对象
    cJSON *root = cJSON_CreateObject();
    if (root == NULL)
    {
        // 如果创建 JSON 对象失败
        log_error("json对象创建失败");
        return Com_FAIL;
    }

    // 使用 snprintf 确保字符串不会溢出
    cJSON_AddStringToObject(root, "action", app_json_data->action);
    cJSON_AddStringToObject(root, "connetType", app_json_data->connetType);
    cJSON_AddNumberToObject(root, "motorId", app_json_data->motorId);
    cJSON_AddNumberToObject(root, "motorSpeed", app_json_data->motorSpeed);
    cJSON_AddStringToObject(root, "status", app_json_data->status);

    // 将 JSON 对象转换为字符串
    char *jsondata = cJSON_PrintUnformatted(root);
    strcpy(data,jsondata);

    //strcpy(data,cJSON_PrintUnformatted(root));

    log_debug("depack data:%s",data);

    // 释放 cJSON 对象
    cJSON_Delete(root);

    // 检查 json_string 是否为 NULL
    if (data == NULL)
    {
        log_error("赋值错误 data == NULL");
        return Com_FAIL;
    }

    // 返回生成的 JSON 字符串
    log_info("json打包成功");
    return Com_OK;
}
